<?php

/**
 * 新增分类
 */
if (!empty($_GET['cid'])) {
    $title = '编辑商品分类-' . $_GET['cid'];
} else {
    $title = '添加商品分类';
}
include "header.php";
if (!empty($_QET['cid'])) {
    $CLASS = $DB->get_row("SELECT * FROM `sky_class` WHERE `cid` = '{$_QET['cid']}' ");
    if (!$CLASS) {
        show_msg('分类->' . $_QET['cid'] . '不存在', '分类->' . $_QET['cid'] . '不存在请检查是否访问有误?', 3);
    }
}
?>
<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12">
                        <h3 class="mb-3 header-title text-success"><?= $title ?></h3>
                        <p class="text-muted">
                            分类图片可使用<a href="http://cloud.79tian.com/" target="_blank">云储</a>存储哦,每个新用户都会赠送免费存储空间！
                        </p>
                        <?php if (!empty($_QET['cid'])) { ?>
                            <div class="row mb-2">
                                <div class="col-sm-4">
                                    <a href="admin.goods.list.php?cid=<?= $_QET['cid'] ?>" class="btn btn-success mb-2"><i class="layui-icon layui-icon-light mr-2"></i>
                                        查看分类下的商品
                                    </a>
                                </div>
                            </div>
                        <?php } ?>
                        <form class="form-horizontal layui-form">
                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">分类状态</label>
                                <select class="custom-select mt-3" name="state">
                                    <option value="1" <?= $CLASS['state'] == 1 ? 'selected' : null ?>>显示分类</option>
                                    <option value="2" <?= $CLASS['state'] == 2 ? 'selected' : null ?>>掩藏分类</option>
                                </select>
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">分类名称</label>
                                <input type="text" name="name" lay-verify="required" class="form-control" value="<?= $CLASS['name'] ?>" placeholder="分类的名称,建议不要起太长">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal">分类图片</label>
                                <div class="input-group">
                                    <input type="text" name="image" lay-verify="required" lay-verType="tips" class="form-control" id="image" value="<?= $_QET['cid'] == '' ? '/assets/img/sc.jpg' : $CLASS['image'] ?>" placeholder="分类的图片,推荐放好看的图片" />
                                    <div class="input-group-append">
                                        <span class="input-group-text" id="upload" style="cursor: pointer">上传</span>
                                    </div>
                                    <div class="input-group-append">
                                        <span class="input-group-text" onclick="layer.alert('<img src=\''+$('#image').val()+'\' style=width:100%  />')" style="cursor: pointer;background-color: slateblue;color: white">预览</span>
                                    </div>
                                    <div class="input-group-append">
                                        <a href="http://cloud.79tian.com/s/e4KSo" target="_blank" class="input-group-text" style="cursor: pointer;background-color: #a8f46b;color: white">图库</a>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">支付通道开关[QQ,微信,支付宝,余额,积分] <a href="javascript:layer.alert('该参数可控制此分类下的所有商品支付通道状态<br>如设置为1,1,1,1,1<br>则相当于开启了QQ,微信,支付宝,余额,积分 五个付款方式<br>如填写2,1,1,1,1<br>则相当于只关闭了QQ，开启了其他支付方式，以此类推<hr><font color=red>商品列表批量操作内可批量设置商品参数，如开启关闭支持的支付通道等！</font><hr>优先级说明：<br>1、当商品关闭了在线支付方式后，此处的QQ,微信,支付宝通道配置均无效,全部算为关闭状态<br>2、当商品开启了余额或积分付款方式后，此处却关闭了余额积分付款方式，用户付款时也会无法使用余额，积分付款，必须两处同时开启！',{title:'填写说明'})">[说明]</a></label>
                                <input type="text" name="support" lay-verify="required" class="form-control" value="<?= $CLASS['support'] ?>" placeholder="1为开启,2为关闭,可填写如：1,1,1,1,1">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">分类公告</label>
                                <textarea class="form-control" name="content" rows="6" placeholder="请输入首页查单公告内容"><?= $CLASS['content'] ?></textarea>
                            </div>

                            <button type="submit" lay-submit lay-filter="hy" class="btn btn-block btn-xs btn-success"><?= $_QET['cid'] == '' ? '添加一个新的分类' : '保存分类信息' ?>
                            </button>
                        </form>
                        <span class="mt-3" style="display: block">官方免费商品图库 <a href="http://cloud.79tian.com/s/e4KSo" target="_blank">进入</a>,存储海量商品图片</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include "bottom.php"; ?>

<script>
    <?php
    if (!empty($_QET['cid'])) {
        echo '
        var dis = ' . $_QET['cid'] . ';';
    } else {
        echo 'var dis = "";';
    }
    ?>
    layui.use(['upload', 'form'], function() {
        var form = layui.form;
        form.on('submit(hy)', function(data) {
            layer.alert('是否要执行当前操作？', {
                icon: 3,
                btn: ['确定', '取消'],
                btn1: function(layero, index) {
                    var index = layer.msg('数据保存中,请稍后...', {
                        icon: 16,
                        time: 999999
                    });
                    if (dis != '') {
                        data.field['dis'] = dis;
                    }
                    $.post('ajax.php?act=add_category', data.field, function(res) {
                        if (res.code == 1) {
                            layer.close(index);
                            layer.alert(res.msg, {
                                btn: ['继续', '返回列表'],
                                btn1: function(layero, index) {
                                    location.reload();
                                },
                                btn2: function(layero, index) {
                                    location.href = 'admin.class.list.php'
                                }
                            });
                        } else {
                            layer.close(index);
                            layer.alert(res.msg, {
                                btn1: function(layero, index) {
                                    location.reload();
                                }
                            });
                        }
                    });
                }
            });
            return false;
        });

        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
                ,
            url: 'ajax.php?act=image_up' //上传接口
                ,
            done: function(res, index, upload) {
                layer.msg('图片上传成功');
                $("input[name='image']").val(res.src);
                $("#image_base").attr('src', res.src);
            },
            error: function() {
                layer.msg('图片上传失败!')
            }
        });
    });
</script>